package com.bird.pro.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bird.pro.mapper.BirdPosttypeMapper;
import com.bird.pro.pojo.BirdPosttype;
import com.bird.pro.pojo.BirdPosttypeExample;
import com.bird.pro.pojo.BirdPosttypeExample.Criteria;
import com.bird.pro.service.IPostTypeService;
import com.bird.pro.vo.PostType;

@Service
@Transactional
public class PostTypeServiceImpl implements IPostTypeService {

	@Autowired
	private BirdPosttypeMapper postTypeMapper;
	
	@Override
	public int saveType(BirdPosttype posttype) {
		if (StringUtils.isNotBlank(posttype.getTypeName())) {
			posttype.setCreateTime(new Date());
			return postTypeMapper.insertSelective(posttype);
		}
		return 0;
	}

	@Override
	public int updateType(BirdPosttype posttype) {
		if (posttype.getId() != null) {
			return postTypeMapper.updateByPrimaryKeySelective(posttype);
		}
		return 0;
	}

	@Override
	public int deleteType(Integer id) {
		if (id != null) {
			BirdPosttypeExample example = new BirdPosttypeExample();
			Criteria criteria = example.createCriteria();
			criteria.andPidEqualTo(id);
			postTypeMapper.deleteByExample(example);
			return postTypeMapper.deleteByPrimaryKey(id);
		}
		return 0;
	}

	@Override
	public List<BirdPosttype> getFirstTypes() {
		BirdPosttypeExample example = new BirdPosttypeExample();
		Criteria criteria = example.createCriteria();
		criteria.andPidIsNull();
		return postTypeMapper.selectByExample(example);
	}

	@Override
	public List<BirdPosttype> getSecondTypes(Integer pid) {
		if (pid != null) {
			BirdPosttypeExample example = new BirdPosttypeExample();
			Criteria criteria = example.createCriteria();
			criteria.andPidEqualTo(pid);
			return postTypeMapper.selectByExample(example);
		}
		return null;
	}

	@Override
	public List<PostType> getPostTypes() {
		List<PostType> postTypes = new ArrayList<>();
		PostType postType = null;
		List<BirdPosttype> chirdTypes = null;
		List<BirdPosttype> posttypes = getFirstTypes();
		for(BirdPosttype type: posttypes){
			postType = new PostType();
			BeanUtils.copyProperties(type, postType);
			
			chirdTypes = getSecondTypes(type.getId());
//			postType.setId(type.getId());
//			postType.setTypeName(type.getTypeName());
			postType.setChildTypes(chirdTypes);
			postTypes.add(postType);
		}
		return postTypes;
	}
	@Override
	public BirdPosttype getTypeByCode(String code){
		BirdPosttypeExample example = new BirdPosttypeExample();
		Criteria criteria = example.createCriteria();
		criteria.andTypeCodeEqualTo(code);
		List<BirdPosttype> types = postTypeMapper.selectByExample(example);
		if (types.isEmpty()) {
			return null;
		}
		return types.get(0);
	}
	
	@Override
	public List<BirdPosttype> getTypes(String code) {
		BirdPosttypeExample example = new BirdPosttypeExample();
		Criteria criteria = example.createCriteria();
		criteria.andTypeCodeEqualTo(code);
		List<BirdPosttype> posttypes = postTypeMapper.selectByExample(example);
		if (!posttypes.isEmpty()) {
			Integer pid = posttypes.get(0).getId();
			return getSecondTypes(pid);
		}
		return null;
	}
	@Override
	public PostType getType(String code){
		PostType postType = new PostType();
		BirdPosttypeExample example = new BirdPosttypeExample();
		Criteria criteria = example.createCriteria();
		criteria.andTypeCodeEqualTo(code);
		List<BirdPosttype> posttypes = postTypeMapper.selectByExample(example);
		if (!posttypes.isEmpty()) {
			BirdPosttype birdPosttype = posttypes.get(0);
			BeanUtils.copyProperties(birdPosttype, postType);
			Integer pid = posttypes.get(0).getId();
			postType.setChildTypes(getSecondTypes(pid));
		}
		return postType;
	}
}
